<?
require_once('version.inc');
require_once('dbaccess.inc');
require_once('language.inc');
require_once('header.inc');
require_once('helper.inc');
require_once('crypt.inc');
require_once('settings.inc');

$knopf = 0;
$func = $_REQUEST['func'];

# Auswertungsmenue
if (isset($_REQUEST['statusb'])) { $knopf = 24; $headline = 26; }

$drm = array(true, false, false, false, false, true, false);

require_once('menu.inc');
require_once('knumber.inc');

# Folgende Funktion erlaubt die Auswahl einer Periode fuer die der Statusbericht
# erzeugt werden soll.
#
function Preselect() {
	global $periode;
	global $unum;
	global $menu;
	global $rstufe;

	$db = OpenDB();
	$query = "select wd_datum from wdone order by wd_datum";
	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return;
	}

	$numrows = numrowsDB($result);

	if ($numrows <= 0) {
	   Error("Es gibt keine IST-Erfassungen!");
	   closeDB($db);
	   return;
	}

	$rows = 0;

	while ($rows < $numrows) {
	   $data = fetchDB($result, $rows);
	   $wd_datum = $data[0];

	   if ($rows == 0)
	      $fdate = $wd_datum;

	   $rows++;
	}

	$ldate = $wd_datum;

	switch($periode) {
	   case 1:		// Jahr
	      $sper = GetPeriode($fdate);
	      $eper = GetPeriode($ldate);
	   break;

	   case 2:		// Quartal
	      $sper = 1;
	      $eper = 4;
	   break;

	   case 3:		// Monat
	      $sper = 1;
	      $eper = 12;
	   break;

	   case 4:		// Woche
	      $sper = 1;
	      $eper = 52;
	   break;

	   case 5:		// Tag
	      $sper = 1;
	      $eper = ($ldate - $fdate) / 86400;
	   break;
	}

	$fyear = gmdate("Y", $fdate);
	$eyear = gmdate("Y", $ldate);
?>
<form action="statusbericht.php" method="post">
<input type="hidden" name="menu" value="<? echo "$menu"; ?>">
<input type="hidden" name="headline" value="26">
<input type="hidden" name="func" value="statusb">

<table border=0>
   <tr>
      <td>Periode:</td>
      <td><select name="per">
<?php
	$mon = gmdate("n", time());

	for ($i = $sper; $i <= $eper; $i++) {
	   if ($i == $mon)
	      echo "            <option selected value=\"$i\">$i</option>\n";
	   else
	      echo "            <option value=\"$i\">$i</option>\n";
	}
?>
         </select>
      </td>
   </tr>
<?php
	if ($periode != 1) {
?>
   <tr>
      <td>Jahr:</td>
      <td><select name="year">
<?php
	   $y = gmdate("Y", time());

	   for ($i = $fyear; $i <= $eyear; $i++) {
	      if ($i == $y)
	         echo "<option selected value=\"$i\">$i</option>\n";
	      else
	         echo "<option value=\"$i\">$i</option>\n";
	   }
?>
          </select>
      </td>
   </tr>
<?php
	}

	# Auswahl eines Projekts
	echo "<tr><td>Projektauswahl:</td>\n";
	echo "<td>";

	if ($rstufe == 2) {		// Projektleiter
	   $query = "select distinct pr_num, pr_name, ks_status from ";
	   $query .= "project, plan, task, allocation, key_status ";
	   $query .= "where pl_prnum = pr_num and ta_plnum = pl_num and ";
	   $query .= "al_task = ta_num and ks_num = pr_status ";
	   $query .= "and (pr_pl = $unum or al_ressource = $unum) ";
	   $query .= "order by pr_num";
	} else if ($rstufe == 3) {	// Mitarbeiter
	   $query = "select distinct wd_prnum, pr_name, ks_status from ";
	   $query .= "wdone, project, key_status where ";
	   $query .= "pr_num = wd_prnum and ks_num = pr_status and ";
	   $query .= "wd_minum = $unum ";
	   $query .= "order by wd_prnum";
	} else {
	   $query = "select pr_num, pr_name, ks_status from ";
	   $query .= "project, key_status where ks_num = pr_status ";
	   $query .= "order by pr_num";
	}

	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return;
	}

	echo "<select name=\"selpro\">\n";
	$numrows = numrowsDB($result);
	$rows = 0;

	while ($rows < $numrows) {
	   $data = fetchDB($result, $rows);
	   $pr_num = $data[0];
	   $pr_name = $data[1];
	   $ks_status = $data[2];
	   echo "<option value=\"$pr_num\">$pr_num $pr_name ($ks_status)</option>\n";
	   $rows++;
	}

	echo "</select></td></tr></table>\n";
	echo "</td></tr>\n";
	echo "</table>\n";
	echo "<br><input type=\"submit\" name=\"submit\" value=\"Senden\"> <input type=\"reset\">\n";
	echo "</form>\n";
	closeDB($db);
}

function ShowISTStatus($print=false) {
	global $leneinheit;
	$per = $_REQUEST['per'];
	$year = $_REQUEST['year'];
	$selpro = $_REQUEST['selpro'];

	$db = OpenDB();
	# Tabelle IST-Aufwaende
?>
<table width="80%" border=1>
   <tr>
      <td rowspan=2 class="stbb">Phase</td>
<?php
	# ermitteln der Laenderspalten
	$query = "select distinct kl_land, mi_land from wdone, mitarbeiter, key_land ";
	$query .= "where wd_prnum = $selpro and mi_num = wd_minum and kl_num = mi_land order by mi_land";
	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return;
	}

	$numrows = numrowsDB($result);
	$rows = 0;
	echo "<td class=\"stbb\" colspan=$numrows>IST-Aufw&auml;nde</td>\n";
	echo "<td class=\"stbb\" rowspan=2>Summe</td></tr>\n<tr>";

	while ($rows < $numrows) {
	   $data = fetchDB($result, $rows);
	   $kl_land = $data[0];
	   $mi_land = $data[1];
	   echo "<td class=\"stb\">$kl_land</td>";
	   $land[$mi_land] = 0;
	   $zwland[$mi_land] = 0;
	   $lpos[$rows] = $mi_land;
	   $rows++;
	}

	echo "</tr>\n";
	$cols = $numrows;

	$days = daysinmonth($per, $year);
	$maxdate = gmmktime(0, 0, 0, $per, $days, $year);
	$query = "select wd_hours, wd_phase, ";
	$query .= "mi_land from wdone, mitarbeiter where wd_prnum = $selpro ";
	$query .= "and mi_num = wd_minum and wd_datum <= $maxdate ";
	$query .= "order by wd_phase, mi_land";
	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return;
	}

	$numrows = numrowsDB($result);
	$rows = 0;

	if ($numrows <= 0) {
	   Error("F&uuml;r das Projekt $selpro existieren keine IST-Daten!<br>");
	   closeDB($db);
	   return;
	}

	$old_phase = -1;
	$sum = 0;
	$total = 0;

	while ($rows < $numrows) {
	   $data = fetchDB($result, $rows);
	   $wd_hours = $data[0];
	   $wd_phase = $data[1];
	   $mi_land = $data[2];

	   if ($old_phase != $wd_phase) {
	      if ($rows > 0) {
	         for ($i = 0; $i < $cols; $i++) {
		    $x = $lpos[$i];
		    printf("<td class=\"stbez\">%s</td>", FormatNum($zwland[$x] / $leneinheit, 3));
		    $zwland[$x] = 0;
		 }

	         printf("<td class=\"stb\">%s</td></tr>\n", FormatNum($sum / $leneinheit, 3));
	      }

	      echo "<tr><td class=\"stb\">$wd_phase</td>";
	      $old_phase = $wd_phase;
	      $sum = 0;
	   }

	   $land[$mi_land] += $wd_hours;
	   $zwland[$mi_land] += $wd_hours;
	   $sum += $wd_hours;
	   $total += $wd_hours;
	   $rows++;
	}

	for ($i = 0; $i < $cols; $i++) {
	   $x = $lpos[$i];
	   printf("<td class=\"stbez\">%s</td>", FormatNum($zwland[$x] / $leneinheit, 3));
	}

	printf("<td class=\"stb\">%s</td></tr>\n", FormatNum($sum / $leneinheit, 3));
	echo "<td class=\"stbb\">Summe</td>";

	for ($i = 0; $i < $cols; $i++) {
	   $x = $lpos[$i];
	   printf("<td class=\"stb\">%s</td>", FormatNum($land[$x] / $leneinheit, 3));
	}

	printf("<td class=\"stbb\">%s</td></tr>\n", FormatNum($total / $leneinheit, 3));
	echo "</table>\n";
	closeDB($db);
}

function ShowRestStatus($print=false) {
	global $leneinheit;
	$per = $_REQUEST['per'];
	$year = $_REQUEST['year'];
	$selpro = $_REQUEST['selpro'];

	$db = OpenDB();
	# Tabelle REST-Aufwaende
?>
<br>
<table width="80%" border=1>
   <tr>
      <td rowspan=3 class="stbb">Phase</td>
<?php
	# ermitteln des gueltigen Plans
	$query = "select pl_lfd from plan where pl_prnum = $selpro and ";
	$query .= "pl_status in (2,3,4) order by pl_lfd desc";
	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return;
	}

	$numrows = numrowsDB($result);

	if ($numrows <= 0) {
	   echo "</table>\n";
	   Error("Es existiert kein gueltiger Plan f&uuml;r diese Auswertung!");
	   closeDB($db);
	   return;
	}

	$data = fetchDB($result, 0);
	$pl_lfd = $data[0];

	# ermitteln der Laenderspalten
	$query = "select distinct kl_land, mi_land from allocation, task, ";
	$query .= "plan, mitarbeiter, key_land where ";
	$query .= "ta_num = al_task and mi_num = al_ressource and ";
	$query .= "pl_num = ta_plnum and pl_prnum = $selpro and ";
	$query .= "pl_lfd = $pl_lfd and kl_num = mi_land order by mi_land";
	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return;
	}

	$numrows = numrowsDB($result);
	$cols = $numrows * 2;
	$rows = 0;
	echo "<td class=\"stbb\" colspan=$cols>REST-Aufw&auml;nde</td>\n";
	echo "<td class=\"stbb\" rowspan=2 colspan=2>Summe</td></tr>\n<tr>";

	while ($rows < $numrows) {
	   $data = fetchDB($result, $rows);
	   $kl_land = $data[0];
	   $mi_land = $data[1];
	   echo "<td class=\"stb\" colspan=2>$kl_land</td>";
	   $land[$mi_land] = 0;
	   $landr[$mi_land] = 0;
	   $zwland[$mi_land] = 0;
	   $lpos[$rows] = $mi_land;
	   $rows++;
	}

	echo "</tr><tr>\n";

	for ($i = 0; $i < ($cols / 2); $i++)
	   echo "<td class=\"stb\">lf.Jahr</td><td class=\"stb\">ff.Jahr</td>\n";

	echo "<td class=\"stbb\">lf.Jahr</td><td class=\"stbb\">ff.Jahr</td></tr>\n";
	$days = daysinmonth($per, $year);
	$maxdate = gmmktime(0, 0, 0, $per, $days, $year);
	$mindate = gmmktime(0, 0, 0, 1, 1, $year);
	$enddate = gmmktime(0, 0, 0, 12, 31, $year);
	$query = "select al_hours, al_phase, mi_land from allocation, task, ";
	$query .= "plan, mitarbeiter, key_land where ";
	$query .= "ta_num = al_task and mi_num = al_ressource and ";
	$query .= "pl_num = ta_plnum and pl_prnum = $selpro and ";
	$query .= "pl_lfd = $pl_lfd and kl_num = mi_land and ";
	$query .= "al_pstart >= $mindate ";
	$query .= "order by al_phase, mi_land, al_pstart";

	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return;
	}

	$numrows = numrowsDB($result);
	$rows = 0;

	if ($numrows <= 0) {
	   Error("F&uuml;r das Projekt $selpro existieren keine geplanten Aufw&auml;nde im Jahr $year!<br>");
	   closeDB($db);
	   return;
	}

	$db2 = OpenDB();
	$old_phase = -1;
	$sum = 0;
	$sumr = 0;
	$total = 0;
	$totalr = 0;

	while ($rows < $numrows) {
	   $data = fetchDB($result, $rows);
	   $al_hours = $data[0];
	   $al_phase = $data[1];
	   $mi_land = $data[2];

	   if (!isset($al_phase))
	      $al_phase = 0;

	   if ($old_phase != $al_phase) {
	      if ($rows > 0) {
		 $sum = 0;
		 $sumr = 0;

	         for ($i = 0; $i < ($cols / 2); $i++) {
		    $x = $lpos[$i];
		    $query = "select sum(wd_hours), count(*) from wdone, mitarbeiter ";
		    $query .= "where wd_prnum = $selpro and mi_num = wd_minum";
		    $query .= " and wd_datum >= $mindate and ";
		    $query .= "wd_datum <= $maxdate and ";
		    $query .= "mi_land = $x and wd_phase = $old_phase";
		    $result2 = QueryDB($db2, $query);

		    if (!$result2) {
		       closeDB($db2);
		       closeDB($db);
		       return;
		    }

		    if (numrowsDB($result2) > 0) {
		       $data = fetchDB($result2, 0);
		       $wd_hours = $data[0];
		       $menge = $data[1];

		       if ($menge == 0)
		          $wd_hours = 0;
		    }
		    else
		       $wd_hours = 0;

		    #
		    # Ermitteln der geplanten Zeiten fuer das folgende Jahr.
		    #
		    $query = "select sum(al_hours), count(*) from allocation, task, ";
		    $query .= "plan, mitarbeiter where mi_land = $x and ";
		    $query .= "ta_num = al_task and mi_num = al_ressource and ";
		    $query .= "pl_num = ta_plnum and pl_prnum = $selpro and ";
		    $query .= "pl_lfd = $pl_lfd and al_pstart > $enddate ";
		    $query .= "and al_phase = $old_phase";
		    $result2 = QueryDB($db2, $query);

		    if (!$result2) {
		       closeDB($db);
		       closeDB($db2);
		       return;
		    }

		    $r = numrowsDB($result2);

		    if ($r > 0) {
		       $data = fetchDB($result2, 0);
		       $resth = $data[0];
		       $menge = $data[1];

		       if ($menge == 0)
		          $resth = 0;

		       $zwland[$x] -= $resth;
		    }
		    else
		       $resth = 0;

		    $wert = ($zwland[$x] - ($wd_hours / $leneinheit));
		    printf("<td class=\"stbez\">%s</td><td class=\"stbez\">%s</td>",
		    	FormatNum($wert, 3), FormatNum($resth, 3));
		    $sum += $wert;
		    $sumr += $resth;
		    $total += $wert;
		    $totalr += $resth;
		    $land[$x] += $wert;
		    $landr[$x] += $resth;
		    $zwland[$x] = 0;
		 }

	         printf("<td class=\"stb\">%s</td><td class=\"stb\">%s</td></tr>\n",
	         	FormatNum($sum, 3), FormatNum($sumr, 3));
	      }

	      echo "<tr><td class=\"stb\">$al_phase</td>";
	      $old_phase = $al_phase;
	   }

	   $zwland[$mi_land] += $al_hours;
	   $rows++;
	}

	$sum = 0;
	$sumr = 0;

	for ($i = 0; $i < ($cols / 2); $i++) {
	   $x = $lpos[$i];
	   $query = "select sum(wd_hours), count(*) from wdone, mitarbeiter ";
	   $query .= "where wd_prnum = $selpro and mi_num = wd_minum";
	   $query .= " and wd_datum >= $mindate and ";
	   $query .= "wd_datum <= $maxdate and ";
	   $query .= "mi_land = $x and wd_phase = $old_phase";

	   if (!$result2) {
	      closeDB($db2);
	      closeDB($db);
	      return;
	   }

	   if (numrowsDB($result2) > 0) {
	      $data = fetchDB($result2, 0);
	      $wd_hours = $data[0];
	      $menge = $data[1];

	      if ($menge == 0)
	         $wd_hours = 0;
	   } else
	      $wd_hours = 0;

	   #
	   # Ermitteln der geplanten Zeiten fuer das folgende Jahr.
	   #
	   $query = "select sum(al_hours), count(*) from allocation, task, ";
	   $query .= "plan, mitarbeiter where mi_land = $x and ";
	   $query .= "ta_num = al_task and mi_num = al_ressource and ";
	   $query .= "pl_num = ta_plnum and pl_prnum = $selpro and ";
	   $query .= "pl_lfd = $pl_lfd and al_pstart > $enddate ";
	   $query .= "and al_phase = $old_phase";
	   $result2 = QueryDB($db2, $query);

	   if (!$result2) {
	      closeDB($db);
	      closeDB($db2);
	      return;
	   }

	   $r = numrowsDB($result2);

	   if ($r > 0) {
	      $data = fetchDB($result2, 0);
	      $resth = $data[0];
	      $menge = $data[1];

	      if ($menge == 0)
	         $resth = 0;

	      $zwland[$x] -= $resth;
	   }
	   else
	      $resth = 0;

	   $wert = ($zwland[$x] - ($wd_hours / $leneinheit));
	   printf("<td class=\"stbez\">%s</td><td class=\"stbez\">%s</td>",
	   	FormatNum($wert, 3), FormatNum($resth, 3));
	   $sum += $wert;
	   $sumr += $resth;
	   $total += $wert;
	   $totalr += $resth;
	   $land[$x] += $wert;
	   $landr[$x] += $resth;
	}

	printf("<td class=\"stb\">%s</td><td class=\"stb\">%s</td></tr>\n",
		FormatNum($sum, 3), FormatNum($sumr, 3));
	echo "<td class=\"stbb\">Summe</td>";

	for ($i = 0; $i < ($cols / 2); $i++) {
	   $x = $lpos[$i];
	   printf("<td class=\"stb\">%s</td><td class=\"stb\">%s</td>",
	   	FormatNum($land[$x], 3), FormatNum($landr[$x], 3));
	}

	printf("<td class=\"stbb\">%s</td><td class=\"stbb\">%s</td></tr>\n",
		FormatNum($total, 3), FormatNum($totalr, 3));
	$i = $cols + 1;
	printf("<tr><td colspan=$i> </td><td colspan=2 class=\"stbb\">%s</td></tr>\n",
		FormatNum($total + $totalr, 3));
	echo "</table>\n";
	closeDB($db);
}

function ShowGesamtStatus($print=false) {
	global $leneinheit;
	$per = $_REQUEST['per'];
	$year = $_REQUEST['year'];
	$selpro = $_REQUEST['selpro'];

	$db = OpenDB();
	# Tabelle REST-Aufwaende
?>
<br>
<table width="80%" border=1>
   <tr>
      <td rowspan=2 class="stbb">Phase</td>
<?php
	# ermitteln des gueltigen Plans
	$query = "select pl_lfd from plan where pl_prnum = $selpro and ";
	$query .= "pl_status in (2,3,4) order by pl_lfd desc";
	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return;
	}

	$numrows = numrowsDB($result);

	if ($numrows <= 0) {
	   echo "</table>\n";
	   Error("Es existiert kein gueltiger Plan f&uuml;r diese Auswertung!");
	   closeDB($db);
	   return;
	}

	$data = fetchDB($result, 0);
	$pl_lfd = $data[0];

	# ermitteln der Laenderspalten
	$query = "select distinct kl_land, mi_land from allocation, task, ";
	$query .= "plan, mitarbeiter, key_land where ";
	$query .= "ta_num = al_task and mi_num = al_ressource and ";
	$query .= "pl_num = ta_plnum and pl_prnum = $selpro and ";
	$query .= "pl_lfd = $pl_lfd and kl_num = mi_land order by mi_land";

	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return;
	}

	$numrows = numrowsDB($result);
	$cols = $numrows;
	$rows = 0;
	echo "<td class=\"stbb\" colspan=$cols>Gesamt-Aufw&auml;nde</td>\n";
	echo "<td class=\"stbb\" rowspan=2>Summe</td></tr>\n<tr>";

	while ($rows < $numrows) {
	   $data = fetchDB($result, $rows);
	   $kl_land = $data[0];
	   $mi_land = $data[1];
	   echo "<td class=\"stb\">$kl_land</td>";
	   $land[$mi_land] = 0;
	   $zwland[$mi_land] = 0;
	   $lpos[$rows] = $mi_land;
	   $rows++;
	}

	echo "</tr><tr>\n";

	$query = "select al_hours, al_phase, mi_land from allocation, task, ";
	$query .= "plan, mitarbeiter, key_land where ";
	$query .= "ta_num = al_task and mi_num = al_ressource and ";
	$query .= "pl_num = ta_plnum and pl_prnum = $selpro and ";
	$query .= "pl_lfd = $pl_lfd and kl_num = mi_land ";
	$query .= "order by al_phase, mi_land";
	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return;
	}

	$numrows = numrowsDB($result);
	$rows = 0;

	if ($numrows <= 0) {
	   Error("F&uuml;r das Projekt $selpro existieren keine Ressourcen!<br>");
	   closeDB($db);
	   return;
	}

	$old_phase = -1;
	$sum = 0;
	$total = 0;
	$days = daysinmonth($per, $year);
	$maxdate = gmmktime(0, 0, 0, $per, $days, $year);

	while ($rows < $numrows) {
	   $data = fetchDB($result, $rows);
	   $al_hours = $data[0];
	   $al_phase = $data[1];
	   $mi_land = $data[2];

	   if ($old_phase != $al_phase) {
	      if ($rows > 0) {
		 $sum = 0;

	         for ($i = 0; $i < $cols; $i++) {
		    $x = $lpos[$i];
		    printf("<td class=\"stbez\">%s</td>", FormatNum($zwland[$x], 3));
		    $sum += $zwland[$x];
		    $total += $zwland[$x];
	   	    $land[$x] += $zwland[$x];
		    $zwland[$x] = 0;
		 }

	         printf("<td class=\"stb\">%s</td></tr>\n", FormatNum($sum, 3));
	      }

	      echo "<tr><td class=\"stb\">$al_phase</td>";
	      $old_phase = $al_phase;
	   }

	   $zwland[$mi_land] += $al_hours;
	   $rows++;
	}

	$sum = 0;

	for ($i = 0; $i < $cols; $i++) {
	   $x = $lpos[$i];
	   printf("<td class=\"stbez\">%s</td>", FormatNum($zwland[$x], 3));
	   $sum += $zwland[$x];
	   $total += $zwland[$x];
	   $land[$x] += $zwland[$x];
	}

	printf("<td class=\"stb\">%s</td></tr>\n", FormatNum($sum, 3));
	echo "<td class=\"stbb\">Summe</td>";

	for ($i = 0; $i < $cols; $i++) {
	   $x = $lpos[$i];
	   printf("<td class=\"stb\">%s</td>", FormatNum($land[$x], 3));
	}

	printf("<td class=\"stbb\">%s</td></tr>\n", FormatNum($total, 3));
	echo "</table>\n";
	closeDB($db);
}

function ShowPlanDate($print=false) {
	global $einheit;
	global $dtformatshort;

	$per = $_REQUEST['per'];
	$year = $_REQUEST['year'];
	$selpro = $_REQUEST['selpro'];

	$db = OpenDB();
	# Tabelle REST-Aufwaende
?>
<br>
<table border=1>
   <tr>
      <td colspan=4 class="stbb">Aktuelle Plantermine</td>
   </tr>
   <tr>
      <td class="stbb">Phase</td>
      <td class="stb">Beginn</td>
      <td class="stb">Ende</td>
      <td class="stb">Tage</td>
   </tr>
<?php
	# Finde als erstes den leuesten plan
	$query = "select pl_lfd from plan where pl_prnum = $selpro ";
	$query .= "order by pl_lfd desc";
	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return;
	}

	$data = fetchDB($result, 0);
	$pl_lfd = $data[0];

	# Hole die Plandaten
	$query = "select ta_start, ta_duration, ta_phase, ta_meeting, ta_level from ";
	$query .= "task, plan where pl_num = ta_plnum and ";
	$query .= "pl_prnum = $selpro and pl_lfd = $pl_lfd ";
	$query .= "order by ta_phase, ta_start";
	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return;
	}

	$numrows = numrowsDB($result);
	$rows = 0;
	$old_phase = -1;
	$sum = 0;
	$sdate = 0;
	$edate = 0;
	$fdate = 0;
	$ldate = 0;

	while ($rows < $numrows) {
	   $data = fetchDB($result, $rows);
	   $ta_start = $data[0];
	   $ta_duration = $data[1];
	   $ta_phase = $data[2];
	   $ta_meeting = $data[3];
	   $ta_level = $data[4];

	   if ($rows == 0)
	      $old_phase = $ta_phase;

	   if ($old_phase != $ta_phase) {
	      $strsd = gmdate($dtformatshort, $sdate);
	      $stred = gmdate($dtformatshort, $edate);
	      $diff = (integer)(($edate - $sdate) / 86400);
	      echo "<tr><td class=\"stb\">$old_phase</td>";
	      echo "<td class=\"stbez\">$strsd</td>";
	      echo "<td class=\"stbez\">$stred</td>";
	      echo "<td class=\"stb\">$diff</td></tr>\n";

	      if ($ldate < $edate)
	         $ldate = $edate;

	      $sdate = 0;
	      $edate = 0;
	      $old_phase = $ta_phase;
	   }

	   if ($sdate == 0)
	      $sdate = $ta_start;

	   if ($fdate == 0)
	      $fdate = $ta_start;

	   if ($ta_meeting == 0 && $ta_level > 1)
	      $dt = GetEDate($einheit, $ta_start, $ta_duration);
	   else if ($ta_level == 0)
	      $dt = 0;
	   else
	      $dt = $ta_start;

	   if ($dt > $edate)
	      $edate = $dt;

	   $rows++;
	}

	$strsd = gmdate($dtformatshort, $sdate);
	$stred = gmdate($dtformatshort, $edate);
	$diff = (integer)(($edate - $sdate) / 86400);
	echo "<tr><td class=\"stb\">$ta_phase</td>";
	echo "<td class=\"stbez\">$strsd</td>";
	echo "<td class=\"stbez\">$stred</td>";
	echo "<td class=\"stb\">$diff</td></tr>\n";

	if ($ldate < $edate)
	   $ldate = $edate;

	$strsd = gmdate($dtformatshort, $fdate);
	$stred = gmdate($dtformatshort, $ldate);
	$diff = (integer)(($ldate - $fdate) / 86400);
	echo "<tr><td class=\"stbb\">Maximal:</td>";
	echo "<td class=\"stb\">$strsd</td>";
	echo "<td class=\"stb\">$stred</td>";
	echo "<td class=\"stb\">$diff</td></tr>\n";
	echo "</table>\n";
}
# Auswertung des Menues:
#
if ($knopf == 24)		// Statusbericht
   Preselect();

# Auswertung der Funktionen
if ($func == "statusb") {
   $per = $_REQUEST['per'];
   $year = $_REQUEST['year'];
   $selpro = $_REQUEST['selpro'];

   # Ermitteln des Projektnamens und Anzeigen des selben.
   $db = OpenDB();
   $query = "select pr_name from project where pr_num = $selpro";
   $result = QueryDB($db, $query);
   $data = fetchDB($result, 0);
   $pr_name = $data[0];
   echo "<p class=\"cry\">Projekt: $selpro $pr_name<br>Zeitraum: $per.$year</p>";
   echo "<br>\n";
   closeDB($db);

   echo "<a href=\"#\" onClick=\"javascript:open_mwindow('statusbericht.php?selpro=$selpro&per=$per&year=$year&header=2&func=PrintStatus', 'Statusbericht: $selpro $pr_name', 750, 550)\">";
   echo "<img src=\"image/print.png\" border=0 alt=\"Drucken\"></a>\n";

   ShowISTStatus();
   ShowRestStatus();
   ShowGesamtStatus();
   ShowPlanDate();
}

if ($func == "PrintStatus") {
   $per = $_REQUEST['per'];
   $year = $_REQUEST['year'];
   $selpro = $_REQUEST['selpro'];

   # Ermitteln des Projektnamens und Anzeigen des selben.
   $db = OpenDB();
   $query = "select pr_name from project where pr_num = $selpro";
   $result = QueryDB($db, $query);
   $data = fetchDB($result, 0);
   $pr_name = $data[0];
   echo "<p class=\"cry\">Projekt: $selpro $pr_name<br>Zeitraum: $per.$year</p>";
   echo "<br>\n";
   closeDB($db);

   ShowISTStatus(true);
   ShowRestStatus(true);
   ShowGesamtStatus(true);
   ShowPlanDate(true);
}

require('footer.inc');
?>
